package com.ym.pms.dao;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ym.pms.model.Comments;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author ming
 * @Description
 * @Date 2020/12/16/016 20:58
 **/
@Repository
public interface CommentsMapper extends BaseMapper<Comments> {
    @Results(id="comment" ,value =
    {
            @Result(column = "com_id",property ="id" ),
            @Result(column = "com_uid",property = "userId"),
            @Result(column = "com_adminid",property = "adminId"),
            @Result(column = "u_name",property = "adminName"),
            @Result(column = "com_title",property = "title"),
            @Result(column = "com_content",property = "content"),
            @Result(column = "com_img",property = "imgPath"),
            @Result(column = "com_isread",property = "isRead"),
            @Result(column = "com_createtime",property = "createTime"),
            @Result(column = "com_rank",property = "rank")
    })
    @Select("select " +
                "com_id, com_uid, com_adminid,com_title,com_content, " +
                " com_isread, com_createtime, com_rank, u_name " +
            "from comments,sys_user " +
            "where " +
                "com_uid=#{id} and com_adminid=u_id" +
            " order by com_rank desc,com_createtime desc")
    List<Comments> findByReceiverId(Integer id);

    @Results({
                    @Result(column = "com_id",property ="id" ),
                    @Result(column = "com_uid",property = "userId"),
                    @Result(column = "com_adminid",property = "adminId"),
                    @Result(column = "u_name",property = "userName"),
                    @Result(column = "com_title",property = "title"),
                    @Result(column = "com_content",property = "content"),
                    @Result(column = "com_img",property = "imgPath"),
                    @Result(column = "com_isread",property = "isRead"),
                    @Result(column = "com_createtime",property = "createTime"),
                    @Result(column = "com_rank",property = "rank")
            })
    @Select("select " +
                "com_id, com_uid, com_adminid, com_title,com_content," +
                " com_isread, com_createtime, com_rank, u_name " +
            "from comments,sys_user " +
            "where " +
                " com_uid=u_id and com_adminid=#{id}" +
            " order by com_createtime desc")
    List<Comments> findBySenderId(Integer id);

    @ResultMap("comment")
    @Insert("insert into comments(com_uid, com_adminid,com_title, com_content, " +
            "com_imgpath, com_isread, com_createtime, com_rank) " +
            "VALUES (#{userId},#{adminId},#{title} ,#{content} ,#{imgPath} ,0,#{createTime} ,#{rank} )")
    int add(Comments comments);

    @Delete("delete from comments where com_id=#{id} limit 1")
    int del(Integer id);

//    设置状态为已读
    @Update("update comments set com_isread=1 where com_id=#{id} limit 1")
    int read(Integer id);

    @ResultMap("comment")
    @Select("select " +
            "com_id, com_uid, com_adminid, com_content, com_title," +
            "com_imgpath, com_isread, com_createtime, com_rank, u_name " +
            "from comments,sys_user " +
            "where " +
            "com_id=#{id} and com_adminid=u_id")
    Comments findById(Integer id);
}
